<?php

namespace App\Controllers\Admin;
use Sentry,User,DB,Code,Machine;
use App;
use BaseController,View,Input,Form,Order,Customer,Redirect,Cache,Wechat;
class CodesController extends \BaseController {

	/**
	 * Display a listing of the resource.
	 * GET /admin/codes
	 *
	 * @return Response
	 */
	public function index()
	{
		//
        $PATH = "http://aebo.qiniudn.com/";
        $METHOD = 'codes';
        $ACTION = 'index';
        $codes = Code::orderBy('created_at', 'DESC')->paginate(20);
        return \View::make('admin.codes.index',compact('METHOD','ACTION','codes','PATH'));
	}

	/**
	 * Show the form for creating a new resource.
	 * GET /admin/codes/create
	 *
	 * @return Response
	 */
	public function create()
	{
		//
        $METHOD = 'codes';
        $ACTION = 'index';
        $machines = Machine::all();
        return \View::make('admin.codes.create',compact('METHOD','ACTION','machines'));
	}

	/**
	 * Store a newly created resource in storage.
	 * POST /admin/codes
	 *
	 * @return Response
	 */
	public function store()
	{
		//
        $machine_id = Input::get('machine');
        $start = $machine_id*10000 + Input::get('start');
        $stop = $machine_id*10000 + Input::get('stop');
        $code = new Code();
        $time = strtotime( Input::get('time'));
        $code->machine_id = $machine_id;
        $code->start = $start;
        $code->stop = $stop;
        $code->month = Input::get('month');
        $code->time =$time;
        $code->save();

        $this->downimg($start,$stop);

        return Redirect::route('admin.codes.index');
	}

	/**
	 * Display the specified resource.
	 * GET /admin/codes/{id}
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
        $code = Code::find($id);
        echo '<请复制到迅雷中批量下载>'.'<br>';
        for($i = $code->start ; $i<=$code->stop ; $i++){
            echo \URL::asset('codes/'.$i.'.jpg').'<br>'; ;
        }
	}

	/**
	 * Show the form for editing the specified resource.
	 * GET /admin/codes/{id}/edit
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 * PUT /admin/codes/{id}
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 * DELETE /admin/codes/{id}
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}
    public function downimg($start,$stop){


        if (Cache::has('token'))
        {
            //
            $token = Cache::get('token');
        }else{
            $token =  Wechat::get_access_token('wx15b76a6695b67114','0576fe84623924f945a3472f116b842c');
            Cache::put('token', $token, 240);
        }


        for($i = $start ; $i<=$stop ; $i++){
            $arr['ticket'] = Wechat::get_pic($token,$i,1);
            $url = $arr['ticket'];
            $curl = curl_init($url);
            $filename = '/var/www/le/public/codes/'.$i.".jpg";
            curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
            $imageData = curl_exec($curl);
            curl_close($curl);
            $tp = @fopen($filename, 'a');
            fwrite($tp, $imageData);
            fclose($tp);
        }


    }
}